<template>
	<view class="project-page">
		<custom-navbar class="navigation" title="我的项目" :isReturnVisible="false" :isAddVisible="false" />
		<view class="search-row">
			<view class="search-bar">
				<!-- 添加v-model绑定搜索关键词 -->
				<input class="search-input" placeholder="输入项目名称" v-model="searchKeyword" />
				<!-- 将搜索图标改为按钮并绑定点击事件 -->
				<view class="search-btn" @click="handleSearch">
					<image src="/static/icons/search.png" class="search-icon"></image>
				</view>
				<image src="/static/icons/mic.png" class="search-icon"></image>
			</view>
		</view>
		<!-- 项目列表 -->
		<view class="project-list">
			<view class="project-item" v-for="(item, index) in filteredProjectList" :key="index">
				<view class="project-header">
					<text class="project-name">{{ item.projectName }}</text>
					<text class="project-price">¥{{ item.price.toFixed(2) }}</text>
				</view>

				<view class="project-details">
					<view class="detail-item">
						<text class="detail-label">项目编号：</text>
						<text class="detail-value">{{ item.code }}</text>
					</view>

					<view class="detail-item">
						<text class="detail-label">剩余数量：</text>
						<text class="detail-value">{{ item.quantity }}</text>
					</view>

					<view class="detail-item">
						<text class="detail-label">有效期至：</text>
						<text class="detail-value">{{ item.expiryDate }}</text>
					</view>

					<view class="project-status" :class="getStatusClass(item.status)">
						{{ getStatusText(item.status) }}
					</view>
				</view>
			</view>
		</view>

		<!-- 空状态提示 -->
		<view class="empty-container" v-if="filteredProjectList.length === 0">
			<image class="empty-image" src="/static/icons/empty.png"></image>
			<view class="empty-text">未找到相关项目</view>
			<view class="empty-subtext">请尝试其他搜索关键词</view>
		</view>
	</view>

</template>

<script setup>
	import customNavbar from '../../components/custom-navbar.vue';
	import {
		ref,
		computed
	} from 'vue';
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		getAllProjectService
	} from '../../api/record';
	// const originalProjectList = ref([{
	// 		projectId: 1,
	// 		code: "xiyang",
	// 		projectName: "吸氧",
	// 		price: 60.0,
	// 		quantity: 14,
	// 		expiryDate: "2025-08-09 11:22:43",
	// 		status: 1
	// 	},
	// 	{
	// 		projectId: 2,
	// 		code: "xiezhufuyao",
	// 		projectName: "协助服药",
	// 		price: 0.0,
	// 		quantity: 59,
	// 		expiryDate: "2025-08-10 11:23:22",
	// 		status: 1
	// 	},
	// 	{
	// 		projectId: 3,
	// 		code: "celiangxuya",
	// 		projectName: "测量血压",
	// 		price: 0.0,
	// 		quantity: 80,
	// 		expiryDate: "2025-07-06 11:23:59",
	// 		status: 3
	// 	},
	// 	{
	// 		projectId: 4,
	// 		code: "xiezhuchuanyi",
	// 		projectName: "协助穿衣",
	// 		price: 10.0,
	// 		quantity: 10,
	// 		expiryDate: "2025-06-26 15:24:59",
	// 		status: 3
	// 	},
	// 	{
	// 		projectId: 5,
	// 		code: "chuangshangxitou",
	// 		projectName: "床上洗头",
	// 		price: 20.0,
	// 		quantity: 10,
	// 		expiryDate: "2025-06-25 00:00:00",
	// 		status: 3
	// 	},
	// 	{
	// 		projectId: 6,
	// 		code: "jianshen",
	// 		projectName: "健身",
	// 		price: 10.0,
	// 		quantity: 1,
	// 		expiryDate: "2025-06-21 11:26:11",
	// 		status: 3
	// 	}
	// ])

	// 添加搜索关键词响应式变量
	const searchKeyword = ref('');

	// 修改为原始记录列表
	const originalProjectList = ref([]);

	// 添加过滤后的记录列表
	const filteredProjectList = computed(() => {
		const keyword = searchKeyword.value.trim();
		if (!keyword) {
			return originalProjectList.value;
		}

		// 支持中文和英文大小写不敏感搜索
		return originalProjectList.value.filter(item => {
			// 直接匹配（中文或英文）
			if (item.projectName.includes(keyword)) {
				return true;
			}

			// 大小写不敏感匹配（处理英文大小写）
			const lowerProjectName = item.projectName.toLowerCase();
			const lowerKeyword = keyword.toLowerCase();
			return lowerProjectName.includes(lowerKeyword);
		});
	});

	// 获取状态文本 - 添加即将过期状态
	const getStatusText = (status) => {
		switch (status) {
			case 1:
				return '未到期';
			case 2:
				return '即将过期';
			case 3:
				return '已过期';
			default:
				return '未知状态';
		}
	};

	// 获取状态样式类 - 添加即将过期样式
	const getStatusClass = (status) => {
		switch (status) {
			case 1:
				return 'status-available';
			case 2:
				return 'status-expiring';
			case 3:
				return 'status-expired';
			default:
				return 'status-unknown';
		}
	};

	const getProjectList = async () => {
		const res = await getAllProjectService();
		console.log(res.data);
		if (res && res.data) {
			originalProjectList.value = res.data
		}
	}

	onLoad(() => {
		getProjectList();
	});
</script>

<style>
	.project-page {
		min-height: 100vh;
		background: url('@/static/background.png') no-repeat;
		/* background-size: cover; */
		padding-bottom: 20px;
		padding-top: 30px;
	}

	.my-navigation {
		margin-bottom: 60px;
		/* border: 1px solid; */
	}

	.search-row {
		display: flex;
		align-items: center;
		padding: 10px 16px 0 16px;
		margin-top: 40px;
		gap: 8px;
	}

	.search-bar {
		flex: 1;
		display: flex;
		align-items: center;
		background: #fff;
		border-radius: 18px;
		box-shadow: 0 2px 8px rgba(91, 184, 172, 0.10);
		padding: 0 10px;
		height: 32px;
		min-width: 0;
	}

	/* 添加搜索按钮样式 */
	.search-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 36px;
		height: 36px;
		border-radius: 50%;
		/* background-color: #F6A031; */
		margin-left: 8px;
	}

	.search-btn .search-icon {
		width: 18px;
		height: 18px;
	}

	.search-icon {
		border: 1;
		width: 18px;
		height: 18px;
		margin-right: 4px;
	}

	.search-input {
		flex: 1;
		border: none;
		background: transparent;
		font-size: 14px;
		color: #333;
		outline: none;
		height: 32px;
		line-height: 32px;
	}

	.project-page {
		min-height: 100vh;
		background: url('@/static/background.png') no-repeat;
		padding-bottom: 20px;
		padding-top: 30px;
	}

	.my-navigation {
		margin-bottom: 60px;
	}

	.search-row {
		display: flex;
		align-items: center;
		padding: 10px 16px 0 16px;
		margin-top: 40px;
		gap: 8px;
	}

	.search-bar {
		flex: 1;
		display: flex;
		align-items: center;
		background: #fff;
		border-radius: 18px;
		box-shadow: 0 2px 8px rgba(91, 184, 172, 0.10);
		padding: 0 10px;
		height: 32px;
		min-width: 0;
	}

	.search-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 36px;
		height: 36px;
		border-radius: 50%;
		margin-left: 8px;
	}

	.search-icon {
		width: 18px;
		height: 18px;
		margin-right: 4px;
	}

	.search-input {
		flex: 1;
		border: none;
		background: transparent;
		font-size: 14px;
		color: #333;
		outline: none;
		height: 32px;
		line-height: 32px;
	}

	.project-list {
		padding: 0 16px;
		margin-top: 20px;
	}

	.project-item {
		background-color: #fff;
		border-radius: 12px;
		padding: 16px;
		margin-bottom: 15px;
		box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
	}

	.project-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding-bottom: 12px;
		border-bottom: 1px solid #f0f0f0;
		margin-bottom: 12px;
	}

	.project-name {
		font-size: 18px;
		font-weight: bold;
		color: #333;
		flex: 1;
	}

	.project-price {
		font-size: 18px;
		font-weight: bold;
		color: #F6A031;
		margin-left: 10px;
	}

	.project-details {
		display: grid;
		grid-template-columns: repeat(2, 1fr);
		gap: 12px;
		position: relative;
	}

	.detail-item {
		display: flex;
	}

	.detail-label {
		font-size: 14px;
		color: #666;
		white-space: nowrap;
	}

	.detail-value {
		font-size: 14px;
		color: #333;
		font-weight: 500;
		margin-left: 5px;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.project-status {
		position: absolute;
		right: 0;
		bottom: 0;
		padding: 4px 10px;
		border-radius: 15px;
		font-size: 12px;
		font-weight: 500;
	}

	/* 可用状态 */
	.status-available {
		background-color: #e8f5e9;
		color: #4caf50;
	}

	/* 即将过期状态 - 新增样式 */
	.status-expiring {
		background-color: #fff3e0;
		color: #ff9800;
	}

	/* 已过期状态 */
	.status-expired {
		background-color: #f5f5f5;
		color: #9e9e9e;
	}

	/* 未知状态 */
	.status-unknown {
		background-color: #e0e0e0;
		color: #616161;
	}

	/* 空状态样式 */
	.empty-container {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 50px 20px;
		text-align: center;
		margin-top: 60px;
	}

	.empty-image {
		width: 150px;
		height: 150px;
		margin-bottom: 20px;
		opacity: 0.8;
	}

	.empty-text {
		font-size: 18px;
		font-weight: 500;
		color: #5a5a5a;
		margin-bottom: 10px;
	}

	.empty-subtext {
		font-size: 14px;
		color: #999;
		line-height: 1.6;
	}
</style>